package org.example.managere.config;

import org.example.managere.dto.FMEAFMESDto;
import org.example.managere.dto.OperationDto;
import org.example.managere.dto.feibiao.AmKnowledgeFailureDto;
import org.example.managere.entity.feibiao.AmKnowledgeFailure;
import org.example.managere.entity.operationdata.FMEAFMES;
import org.example.managere.entity.operationdata.Operation;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ModelMapperConfig {
    @Bean
    public ModelMapper modelMapper() {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

        // 设置AmKnowledgeFailure和AmKnowledgeFailureDto之间的映射
        modelMapper.createTypeMap(AmKnowledgeFailure.class, AmKnowledgeFailureDto.class)
                .addMappings(mapper -> {
                    mapper.skip(AmKnowledgeFailureDto::setMotorId);
                    mapper.skip(AmKnowledgeFailureDto::setMotorType);
                    mapper.skip(AmKnowledgeFailureDto::setManufacturer);
                });

        // 设置FMEAFMES和FMEAFMESDto之间的映射
        modelMapper.createTypeMap(FMEAFMES.class, FMEAFMESDto.class)
                .addMappings(mapper -> {
                    mapper.map(src -> src.getSystem(), FMEAFMESDto::setSystemName);
                });

        modelMapper.createTypeMap(FMEAFMESDto.class, FMEAFMES.class)
                .addMappings(mapper -> {
                    mapper.map(src -> src.getSystemName(), FMEAFMES::setSystem);
                });
                
        // 设置Operation和OperationDto之间的映射
        modelMapper.createTypeMap(Operation.class, OperationDto.class)
                .addMappings(mapper -> {
                    mapper.map(src -> src.getSystem(), OperationDto::setSystem);
                    mapper.map(src -> src.getUpdateTime(), OperationDto::setUpdateTime);
                    mapper.map(src -> src.getFMode(), OperationDto::setFMode);
                    mapper.map(src -> src.getFCause(), OperationDto::setFCause);
                    mapper.map(src -> src.getFEffects(), OperationDto::setFEffects);
                });

        modelMapper.createTypeMap(OperationDto.class, Operation.class)
                .addMappings(mapper -> {
                    mapper.map(src -> src.getSystem(), Operation::setSystem);
                    mapper.map(src -> src.getUpdateTime(), Operation::setUpdateTime);
                    mapper.map(src -> src.getFMode(), Operation::setFMode);
                    mapper.map(src -> src.getFCause(), Operation::setFCause);
                    mapper.map(src -> src.getFEffects(), Operation::setFEffects);
                });

        return modelMapper;
    }
}